﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommDll;

namespace HospitalMedicineTraceability
{
    /// <summary>
    /// ClassName:frmQuerySelect
    /// Version:1.0
    /// Date:2024-07-09
    /// Author:seanouyang
    /// </summary>
    /// <remarks>
    /// 本类是一WinForm 窗体,本窗体主要为实现相关信息查询
    /// </remarks>
    public partial class frmQuerySelect : DevComponents.DotNetBar.OfficeForm
    {
        #region 01.自定义属性
        public int m_nKind = 0;// 1:配企查询 10:His商品查询
        public string m_strInfo = "";
        public string m_strWhere = "";
        public DataGridViewRow m_dgvrCurrentRow = null;  // 选中行
        public Dictionary<string, string> m_dictSelected = new Dictionary<string, string>();
        #endregion

        public frmQuerySelect()
        {
            InitializeComponent();
        }

        #region 03.系统事件方法
        private void dataGridViewX1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // 设置行序号
            DataGridViewRow CurrentRow = this.dataGridViewX1.Rows[e.RowIndex];
            CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex + 1);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (dataGridViewX1.DataSource != null)
            {
                ((DataTable)dataGridViewX1.DataSource).Rows.Clear();
            }
            try
            {
                // 01 取数据
                string strSql = "";
                string strDataGridViewTitle = "";
                string strDataGridViewWidth = "";
                switch (m_nKind){
                    case 1:
                        strSql = "select * from (select SupplierCode,SupplierName FROM TH_StockIn group by SupplierCode,SupplierName) A where 1=1 {0} {1}";
                        strDataGridViewTitle = "配企编号,配企名称";
                        strDataGridViewWidth = "150,250";
                        strSql = string.Format(strSql, string.IsNullOrWhiteSpace(txtInfo.Text.Trim()) ? "" : string.Format(" and (SupplierCode like '%{0}%' or SupplierName like '%{0}%' or dbo.F_GetSpellCN(SupplierName) like '%{0}%')", txtInfo.Text.Trim())
                            ,m_strWhere);                        
                        break;
                    case 10:
                        // 药品对码
                        strSql = "select * from (select Id,Code,Name,PackageSpec,Manufacturer,Unit FROM TH_Goods where isnull(Status,1) = 1 and '{0}' = '' or (Code like '%{0}%' or Name like '%{0}%' or dbo.F_GetSpellCN(Name) like '%{0}%' or Manufacturer like '%{0}%')) A {1}";
                        strDataGridViewTitle = "序号,药品编号,药品名称,规格,生产厂家,单位";
                        strDataGridViewWidth = "0,100,200,120,250,80";
                        // 条件1
                        strSql = string.Format(strSql, txtInfo.Text.Trim(), m_strWhere);
                        break;
                }
                
                DataTable dt = null;
                dt = AppInfo._openSql_DataTable(strSql);

                dataGridViewX1.DataSource = dt;
                // 02 设置列名称及宽度
                CommFun._setColumnNameDataGridView_String(dataGridViewX1, strDataGridViewTitle);
                CommFun._setColumnWidthDataGridView_String(dataGridViewX1, strDataGridViewWidth);
            }
            catch (Exception ex)
            {
                CommFun._messageBoxShow(ex.Message);
                return;
            }
        }               

        private void _Init()
        {
            // 自动调用数据
            btnQuery_Click(null, null);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void txtInfo_KeyDown(object sender, KeyEventArgs e)
        {
            // 回车后直接查询
            if (e.KeyValue == 13)
            {
                btnQuery_Click(null, null);
            }
        }

        private void frmQuerySelect_Shown(object sender, EventArgs e)
        {
            if (!m_strWhere.Equals(""))
            {
                _Init();
            }            
        }

        private void frmQuerySelect_Load(object sender, EventArgs e)
        {
            txtInfo.Text = m_strInfo;
            switch (m_nKind)
            {
                case 1:
                    lblTitle.Text = "请确定配企信息";
                    label5.Text = "配企信息";
                    btnQuery.Text = "配企查询(&Q)";
                    break;
                case 2:
                    lblTitle.Text = "请确定配企药品信息";
                    break;
                case 10:
                    lblTitle.Text = "请确定HIS药品信息";
                    break;
                default:
                    break;
            }            
            this.Text = lblTitle.Text;
                    
            int height = System.Windows.Forms.SystemInformation.WorkingArea.Height;
            int width = System.Windows.Forms.SystemInformation.WorkingArea.Width;
            int newformx = width / 2 - Width / 2;
            int newformy = height / 2 - Height / 2;
            this.SetDesktopLocation(newformx, newformy);
            AppInfo.AnimateWindow(this.Handle, 300, AppInfo.AW_CENTER | AppInfo.AW_ACTIVE | AppInfo.AW_VER_NEGATIVE);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewX1.CurrentRow == null){
                switch (m_nKind)
                {
                    case 1:
                        CommFun._messageBoxShow("请先确认所选中配企数据");
                        break;
                    case 2:
                        CommFun._messageBoxShow("请先确认所选中His药品信息");
                        break;
                    case 10:
                        CommFun._messageBoxShow("请先确认所选中His药品信息");
                        break;
                    default:
                        break;
                }
                return;
            }

            if (CommFun._messageBoxShow("确认所选中数据？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_dgvrCurrentRow = this.dataGridViewX1.CurrentRow;
                m_dictSelected.Clear();
                switch (m_nKind)
                {
                    case 1:
                        m_dictSelected.Add("SupplierCode", this.dataGridViewX1.CurrentRow.Cells["SupplierCode"].Value.ToString());
                        m_dictSelected.Add("SupplierName", this.dataGridViewX1.CurrentRow.Cells["SupplierName"].Value.ToString());
                        break;
                    case 10:
                        m_dictSelected.Add("Code", this.dataGridViewX1.CurrentRow.Cells["Code"].Value.ToString());
                        m_dictSelected.Add("Name", this.dataGridViewX1.CurrentRow.Cells["Name"].Value.ToString());
                        m_dictSelected.Add("PackageSpec", this.dataGridViewX1.CurrentRow.Cells["PackageSpec"].Value.ToString());
                        m_dictSelected.Add("Manufacturer", this.dataGridViewX1.CurrentRow.Cells["Manufacturer"].Value.ToString());
                        m_dictSelected.Add("Unit", this.dataGridViewX1.CurrentRow.Cells["Unit"].Value.ToString());
                        break;
                    default:
                        break;
                }
                DialogResult = DialogResult.OK;
            }                    
        }

        private void dataGridViewX1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0) {
                btnSave_Click(null,null);
            }
        }
       
    }
}